const path = require('path')

/**
 * @description: 根据vant组件名，生成相应的映射地址
 * @param {*} componentName：组件名
 * @return {*} 组件映射地址
 */
const createVantPatterns = componentName => {
  const fileTypes = ['wxml', 'wxs', 'wxss']
  return fileTypes.map(item => {
    return {
      from: `src/components/vant-weapp/dist/${componentName}/index.${item}`,
      to: `dist/components/vant-weapp/dist/${componentName}/index.${item}`
    }
  })
}
const config = {
  env: {
    NODE_ENV: process.env.NODE_ENV
  },
  projectName: 'distribution_app',
  date: '2021-9-17',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  plugins: [],
  defineConstants: {},
  alias: {
    '@/api': path.resolve(__dirname, '..', 'src/api'),
    '@/src': path.resolve(__dirname, '..', 'src'),
    // "@/servers": path.resolve(__dirname, "..", "servers"),
    // "@/resource": path.resolve(__dirname, "..", "src/resource"),
    '@/utils': path.resolve(__dirname, '../', 'src/utils'),
    '@/constants': path.resolve(__dirname, '../', 'src/constants'),
    '@/components': path.resolve(__dirname, '..', 'src/components')
  },
  copy: {
    patterns: [
      { from: 'src/static/', to: 'dist/static/' },
      {
        from: 'src/assets/tabbar/my-active.png',
        to: 'dist/static/my-active.png'
      },
      { from: 'src/assets/tabbar/my-gray.png', to: 'dist/static/my-gray.png' },
      {
        from: 'src/assets/tabbar/home-active.png',
        to: 'dist/static/home-active.png'
      },
      {
        from: 'src/assets/tabbar/home-gray.png',
        to: 'dist/static/home-gray.png'
      },
      {
        from: 'src/assets/tabbar/order-active.png',
        to: 'dist/static/order-active.png'
      },
      {
        from: 'src/assets/tabbar/order-gray.png',
        to: 'dist/static/order-gray.png'
      },
      // vant-weapp 组件 /**此处为公共组件，必需--start */
      {
        from: 'src/components/vant-weapp/dist/wxs',
        to: 'dist/components/vant-weapp/dist/wxs'
      },
      {
        from: 'src/components/vant-weapp/dist/common/style',
        to: 'dist/components/vant-weapp/dist/common/style'
      },
      {
        from: 'src/components/vant-weapp/dist/common/index.wxss',
        to: 'dist/components/vant-weapp/dist/common/index.wxss'
      },
      /**此处为公共组件，必需--end */
      ...createVantPatterns('picker-column'),
      ...createVantPatterns('field')

      // { from: 'src/components/vant-weapp/dist/rate/index.wxs',
      // to: 'dist/components/vant-weapp/dist/rate/index.wxs' },
      // { from: 'src/components/vant-weapp/dist/rate/utils.wxs',
      // to: 'dist/components/vant-weapp/dist/rate/utils.wxs' },
      // { from: 'src/components/vant-weapp/dist/rate/calendar.wxml',
      // to: 'dist/components/vant-weapp/dist/calendar/calendar.wxml' },
      // { from: 'src/components/vant-weapp/dist/calendar/components/month/index.wxs',
      // to: 'dist/components/vant-weapp/dist/calendar/components/month/index.wxs' },

      // { from: 'src/components/vant-weapp/dist/calendar/index.wxs',
      // to: 'dist/components/vant-weapp/dist/calendar/index.wxs' },
      // { from: 'src/components/vant-weapp/dist/calendar/utils.wxs',
      // to: 'dist/components/vant-weapp/dist/calendar/utils.wxs' },
      // { from: 'src/components/vant-weapp/dist/calendar/calendar.wxml',
      // to: 'dist/components/vant-weapp/dist/calendar/calendar.wxml' },
      // { from: 'src/components/vant-weapp/dist/calendar/components/month/index.wxs',
      // to: 'dist/components/vant-weapp/dist/calendar/components/month/index.wxs' },
    ],
    options: {}
  },
  framework: 'react',
  mini: {
    postcss: {
      pxtransform: {
        enable: true,
        config: {
          selectorBlackList: [/van-/]
        }
      },
      url: {
        enable: true,
        config: {
          limit: 1024 // 设定转换尺寸上限
        }
      },
      cssModules: {
        enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
    // imageUrlLoaderOption: {
    //   limit: 10,
    //   name: 'assets/[name].[hash:base64:20].[ext]',
    //   publicPath: projectPublicPath,
    // },
    // miniCssExtractPluginOption: {
    //   ignoreOrder: true,
    // },
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {}
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    },
    esnextModules: ['taro-ui', 'taro-skeleton']
  }
}

module.exports = function(merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}
